<script setup>
import axios from 'axios';
import { onMounted, ref } from 'vue';

// 定义数据容器
const list = ref([]);
// 定义请求数据的方法
const getList = async () => {
  const res = await axios.get('/list');
  console.log(res.data);
  // 设置页面数据
  list.value = res.data;
};
// 在组件渲染完毕后发起请求
onMounted(() => {
  getList();
});

// 删除
const delRow = async (id) => {
  console.log('发起删除请求');
  const res = await axios.delete(`/del?id=${id}`);
  console.log(res);
  // 重新请求
  getList();
};
</script>

<template>
  <div>
    <!-- 搭页面结构 -->
    <el-table :data="list">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="籍贯" prop="place"></el-table-column>
      <el-table-column label="操作">
        <!-- 插槽 -->
        <template v-slot="{ row }">
          <el-button type="primary" link @click="delRow(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style scoped></style>
